//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.tools.acceleratecore.xlib.xlib.intf.IXTimer;
import com.tools.acceleratecore.xlib.xlib.intf.IXTimerListener;
import java.util.Timer;
import java.util.TimerTask;

public class XTimer implements IXTimer {
    private Timer mTimer = null;
    private IXTimerListener mIXTimerListener = null;
    private Handler mHandler = null;
    private boolean mWork = false;
    private Object mObjLock = null;
    private static final int VALUE_INT_MESSAGEID_TIMER = 4096;
    private static final int VALUE_INT_MESSAGEID_TIMER_REPEAT = 4097;

    public XTimer() {
        this._init();
    }

    private void _init() {
        this.mObjLock = new Object();
        this.mHandler = new Handler(Looper.getMainLooper()) {
            public void handleMessage(Message msg) {
                if (4096 == msg.what) {
                    if (XTimer.this.mIXTimerListener != null) {
                        XTimer.this.mIXTimerListener.onTimerComplete();
                    }

                    XTimer.this.clear();
                } else if (4097 == msg.what && XTimer.this.mIXTimerListener != null) {
                    XTimer.this.mIXTimerListener.onTimerComplete();
                }

            }
        };
    }

    public boolean start(int nDelayTime, IXTimerListener iXTimerListener) {
        Object var3 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (!this.mWork && iXTimerListener != null) {
                this.mWork = true;
                this.mIXTimerListener = iXTimerListener;
                this.mTimer = new Timer();
                this.mTimer.schedule(new TimerTask() {
                    public void run() {
                        Message msg = new Message();
                        msg.what = 4096;
                        XTimer.this.mHandler.sendMessage(msg);
                    }
                }, (long)nDelayTime);
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean start(int nDelayTime, int nSpacingTime, IXTimerListener iXTimerListener) {
        Object var4 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (!this.mWork && iXTimerListener != null) {
                this.mWork = true;
                this.mIXTimerListener = iXTimerListener;
                this.mTimer = new Timer();
                this.mTimer.schedule(new TimerTask() {
                    public void run() {
                        Message msg = new Message();
                        msg.what = 4097;
                        XTimer.this.mHandler.sendMessage(msg);
                    }
                }, (long)nDelayTime, (long)nSpacingTime);
                return true;
            } else {
                return false;
            }
        }
    }

    public void stop() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (this.mWork && this.mTimer != null) {
                this.mTimer.cancel();
            }
        }

        this.clear();
    }

    public boolean isWork() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            return this.mWork;
        }
    }

    private void clear() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            this.mWork = false;
            this.mTimer = null;
            this.mIXTimerListener = null;
        }
    }
}
